﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Com.Rivyer.Model;
namespace Com.Rivyer.BLL
{
    /// <summary>
    /// tmppinglistBll
    /// </summary>
    public partial class tmppinglistBll
    {
        private readonly Com.Rivyer.DAL.tmppinglistDal dal = new Com.Rivyer.DAL.tmppinglistDal();
        private readonly Com.Rivyer.BLL.monitorBll monitorbll = new monitorBll();
        public tmppinglistBll()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Com.Rivyer.Model.tmppinglistModel model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Com.Rivyer.Model.tmppinglistModel model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Com.Rivyer.Model.tmppinglistModel GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Com.Rivyer.Model.tmppinglistModel GetModelByCache(int id)
        {

            string CacheKey = "tmppinglistModelModel-" + id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Com.Rivyer.Model.tmppinglistModel)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Com.Rivyer.Model.tmppinglistModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Com.Rivyer.Model.tmppinglistModel> DataTableToList(DataTable dt)
        {
            List<Com.Rivyer.Model.tmppinglistModel> modelList = new List<Com.Rivyer.Model.tmppinglistModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Com.Rivyer.Model.tmppinglistModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Com.Rivyer.Model.tmppinglistModel();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["username"] != null && dt.Rows[n]["username"].ToString() != "")
                    {
                        model.username = dt.Rows[n]["username"].ToString();
                    }
                    if (dt.Rows[n]["description"] != null && dt.Rows[n]["description"].ToString() != "")
                    {
                        model.description = dt.Rows[n]["description"].ToString();
                    }
                    if (dt.Rows[n]["ip"] != null && dt.Rows[n]["ip"].ToString() != "")
                    {
                        model.ip = dt.Rows[n]["ip"].ToString();
                    }
                    if (dt.Rows[n]["latestpingdate"] != null && dt.Rows[n]["latestpingdate"].ToString() != "")
                    {
                        model.latestpingdate = DateTime.Parse(dt.Rows[n]["latestpingdate"].ToString());
                    }
                    if (dt.Rows[n]["pingstatus"] != null && dt.Rows[n]["pingstatus"].ToString() != "")
                    {
                        model.pingstatus = int.Parse(dt.Rows[n]["pingstatus"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method

        public void AddSelected(int select, string username)
        {
            Model.monitorModel monitormodel = monitorbll.GetModel(select);
            Model.tmppinglistModel listmodel = new tmppinglistModel();
            if (monitormodel.ip != null
                && Maticsoft.Common.PageValidate.IsIp(monitormodel.ip)
                && dal.GetList(string.Format("ip = '{0}' and username = '{1}'", monitormodel.ip, username)).Tables[0].Rows.Count == 0)
            {
                listmodel.description = monitormodel.department + (monitormodel.address == null ? "" : monitormodel.address);
                listmodel.ip = monitormodel.ip;
                listmodel.username = username;
                dal.Add(listmodel);
            }
        }

        public void DeleteByUsername(string username)
        {
            System.Data.DataSet pinglist = dal.GetList(string.Format("username = '{0}'", username));
            if (pinglist.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < pinglist.Tables[0].Rows.Count; i++)
                {
                    Delete(int.Parse(pinglist.Tables[0].Rows[i]["id"].ToString()));
                }
            }
        }

        public void PingFast(string username)
        {
            List<Model.tmppinglistModel> listmodels = GetModelList(string.Format("username = '{0}'", username));
            foreach (Model.tmppinglistModel listmodel in listmodels)
            {
                listmodel.latestpingdate = DateTime.Now;
                listmodel.pingstatus = Maticsoft.Common.netping.pingIP(listmodel.ip) == true ? 1 : 0;
                Update(listmodel);
            }
        }
    }
}

